package com.jintoufs.web.action.tour;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.tour.TourRouteOrderDetailMapper;
import com.jintoufs.domain.tour.TourRoute;
import com.jintoufs.domain.tour.TourRouteOrder;
import com.jintoufs.domain.tour.TourRouteOrderDetail;
import com.jintoufs.logAnnotation.SystemControllerLog;
import com.jintoufs.service.tour.TourRouteOrderService;
import com.jintoufs.service.tour.TourRouteService;
import com.jintoufs.web.action.support.JqgridPage;
import com.jintoufs.web.action.support.SupportAction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("tour/order")
public class TourRouteOrderAction extends SupportAction {
    @Resource
    private TourRouteOrderService tourRouteOrderService;
    @Resource
    private TourRouteService tourRouteService;

    @RequestMapping("list")
    public String list(Model model) {
        return "tour/order/list";
    }

    @Resource
    private TourRouteOrderDetailMapper tourRouteOrderDetailMapper;

    @RequestMapping("query")
    @ResponseBody
    @SystemControllerLog(description = "获取路线订单列表")
    public JqgridPage query(int page,
                            int rows, String sidx,
                            String sord,
                            String filters) {
        QueryFilters filters_;
        PageHelper.startPage(page, rows);
        if (filters == null || filters.trim().isEmpty()) {
            filters_ = new QueryFilters();
        } else {
            filters_ = JSON.parseObject(filters, QueryFilters.class);
        }
        filters_.setSidx(sidx);
        filters_.setSord(sord);
        List<Map<String,Object>> tour = tourRouteOrderService.queryOrderByFilters(filters_);
        PageInfo pageInfo = new PageInfo(tour);
        return new JqgridPage(pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum(),
                pageInfo.getList());
    }



    @RequestMapping(value = "/detail")
    public String detail(String id, Model model) {
        List<TourRouteOrderDetail> orderDetail = tourRouteOrderService.selectDetailByRouteOrderId(id);
        Map<String, Object> routeOrder = tourRouteOrderService.selectById(id);
        model.addAttribute("orderDetails", orderDetail);
        model.addAttribute("routeOrder", routeOrder);
        return "tour/order/detail";
    }

}
